<template>
  <!-- 顶部 -->
  <el-container class="home_container">
    <!-- 头部 -->
    <el-header>
      <div>
        <!-- logo -->
        <img src="../assets/heimalogo.jpeg" alt="" style="width:100px;height:56px">
        <span>电商后台管理系统</span>
      </div>
      <!-- 退出按钮 -->
      <el-button type="primary" size='mini' @click="logout">退出</el-button>
    </el-header>
    <!-- 侧边栏 -->
    <el-container>
      <!-- 侧边栏的伸缩按钮 -->
      <el-aside :width="isCollapse? '65px':'200px'">
        <div class="
        toggle" @click="toggleBtn"><i class="el-icon-s-operation fontsize"></i></div>
        <!-- 侧边栏菜单 -->
        <el-menu :collapse="isCollapse" :collapse-transition="false" background-color="#333744" text-color="#fff"
          active-text-color="#409EFF" router :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index='item.id + ""' v-for="item in menulist" :key="item.id">
            <!-- 一级菜单模版 -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index='"/"+item1.path' v-for="item1 in item.children" :key="item1.id" @click="changeActivePath(
              '/'+item1.path)">
              <!-- 二级菜单模版 -->
              <template slot="title">
                <span>{{item1.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      activePath: '',
      menulist: [],
      iconsObj: {
        125: 'el-icon-s-custom',
        103: 'el-icon-s-fold',
        101: 'el-icon-s-cooperation',
        102: 'el-icon-s-order',
        145: 'el-icon-s-data'
      },
      isCollapse: false
    }
  },
  methods: {
    changeActivePath (path) {
      this.activePath = path
      window.sessionStorage.setItem('activePath', path)
    },
    // 退出按钮
    logout () {
      window.sessionStorage.clear() // 销毁token
      this.$router.push('/login')
    },
    // 页面进去的加载
    async getMenus () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      this.menulist = res.data
    },
    // 伸缩按钮
    toggleBtn () {
      this.isCollapse = !this.isCollapse
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { // 钩子函数  页面打开就会执行
    this.getMenus()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
.home_container {
  height: 100%;
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 20px;
    > div {
      display: flex;
      align-items: center;
      span {
        margin-left: 15px;
        color: #fff;
        font-size: 16px;
      }
    }
  }
  .el-aside {
    background-color: #333744;
  }
}
.el-menu {
  border-right: none;
}
.toggle {
  background-color: #a8abb7;
  text-align: center;
  color: #fff;
  letter-spacing: 0.2em;
  cursor: pointer;
  line-height: 30px;
  height: 30px;
}
.el-main {
  background-color: #eaedf1;
}
.fontsize {
  font-size: 20px;
}
</style>
